package com.asm.service.impl;


import com.asm.mapper.Analysis2With3DMapper;
import com.asm.pojo.Analysis2With3D;
import com.asm.pojo.Gene3D;
import com.asm.pojo.MergedInter;
import com.asm.service.Analysis2With3DService;
import com.asm.util.PageInfoCustom;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class Analysis2With3DServiceImpl implements Analysis2With3DService {

    @Autowired
    private Analysis2With3DMapper analysis2With3DMapper;

    @Override
    public List<Analysis2With3D> getDistal(String[] snvList, int pageNum, int pageSize,String tissue) {
        int offset = (pageNum - 1) * pageSize;
        List<String> rList = Arrays.stream(snvList).distinct().collect(Collectors.toList());
        List<String> files = rList.stream()
                .map(s -> getAllDistalFiles(s))
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        if(files.isEmpty()){
            return Collections.emptyList();
        }

        List<Analysis2With3D> results = analysis2With3DMapper.getDistal(snvList,files,pageNum,pageSize,tissue);
//        int total = analysis2With3DMapper.getTotalCount();
        int total = 0;
        return results;
    }

    @Override
    public List<Analysis2With3D> getProximal(String[] snvList, int pageNum, int pageSize,String tissue) {
        int offset = (pageNum - 1) * pageSize;
        List<String> rList = Arrays.stream(snvList).distinct().collect(Collectors.toList());
        List<String> files =rList.stream()
                .map(s -> getAllProximalFiles(s))
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        if(files.isEmpty()){
            return Collections.emptyList();
        }

        List<Analysis2With3D> results =  analysis2With3DMapper.getProximal(snvList,files,pageNum,pageSize,tissue);
        int total = analysis2With3DMapper.getTotalCount();
        return results;
    }

    @Override
    public List<MergedInter> getMergedInter(String snv) {
        // 查询索引表，获取 file 字段值
        List<String> files = analysis2With3DMapper.get3DPlotFileBySnv(snv);
        List<MergedInter> result = files.stream()
                .map(file -> analysis2With3DMapper.getMergedInterDataBySnv("analysis2_plot_data_" + file, snv))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
        // 查询具体数据表
        return result;
    }

    @Override
    public List<Analysis2With3D> getDistalBySnv(String snvId,String tissue) {
        List<String> files = getAllDistalFiles(snvId);
        if (files.isEmpty()) {
            return Collections.emptyList();
        }
        return analysis2With3DMapper.getDistalbysnv(snvId, files,tissue);
    }

    @Override
    public List<Analysis2With3D> getProximalBySnv(String snvId,String tissue) {
        List<String> files = getAllProximalFiles(snvId);
        if (files.isEmpty()) {
            return Collections.emptyList();
        }
        return analysis2With3DMapper.getProximalbysnv(snvId, files,tissue);
    }

    @Override
    public List<Analysis2With3D> getSNVByIndex(String[] indexsnvId) {
        return analysis2With3DMapper.getSNVByIndex(indexsnvId);
    }

    @Override
    public List<Analysis2With3D> getDistalBySnvList(String[] snvList) {
        return analysis2With3DMapper.getDistalBySnvList(snvList);
    }

    @Override
    public List<Analysis2With3D> getProximalBySnvList(String[] snvList) {
        return analysis2With3DMapper.getProximalBySnvList(snvList);
    }

    @Override
    public List<Gene3D> getGene3D(String snv) {
        List<String> tables = analysis2With3DMapper.getGene3DTable(snv)
                .stream()
                .map(table -> "detail_gene_3D_" + table)
                .collect(Collectors.toList());
        List<Gene3D> result = tables.stream()
                .map(table -> analysis2With3DMapper.getGene3D(snv,table))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
        return result;
    }

    // 获取所有可能的distal file值
    private List<String> getAllDistalFiles(String snv) {
//        List<String> files = new ArrayList<>();
        List<String> file = analysis2With3DMapper.getDistalFileBySnv(snv);
//        去重返回
        return file;
    }

    // 获取所有可能的proximal file值
    private List<String> getAllProximalFiles(String snv) {
//        List<String> files = new ArrayList<>();
        List<String> file = analysis2With3DMapper.getProximalFileBySnv(snv);
//       去重返回
        return file;
    }
}
